﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-08 15:18:15
 * 功能：界面【HallPanel】的View
        1、负责收集所有控件
**************************************************************************************/

using DG.Tweening;
using Game.Framework;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.HallPanel
{
    public class HallPanelView : BasePanel
    {
        public const string PANEL_KEY = "Game_UI_HallPanel_HallPanel_Key";
        private RecordView m_RecordView;
        private GalleryView m_GalleryView;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
            EventCenter.Instance.AddEventListen<long, long>(GameEventName.OnPlayerStarChanged, OnPlayerStarChanged);
            EventCenter.Instance.AddEventListen<long, long>(GameEventName.OnPlayerCoinsChanged, OnPlayerCoinsChanged);
            EventCenter.Instance.AddEventListen<int>(GameEventName.OnCurLevelChanged, OnCurLevelChanged);
            EventCenter.Instance.AddEventListen<GameDataModel.AssetInfo>(GameEventName.OnChangedHallBg, OnChangedHallBg);
            EventCenter.Instance.AddEventListen(GameEventName.JumpMapTab, OnJumpMapTab);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
            EventCenter.Instance.RemoveEventListen<long, long>(GameEventName.OnPlayerStarChanged, OnPlayerStarChanged);
            EventCenter.Instance.RemoveEventListen<long, long>(GameEventName.OnPlayerCoinsChanged, OnPlayerCoinsChanged);
            EventCenter.Instance.RemoveEventListen<int>(GameEventName.OnCurLevelChanged, OnCurLevelChanged);
            EventCenter.Instance.RemoveEventListen<GameDataModel.AssetInfo>(GameEventName.OnChangedHallBg, OnChangedHallBg);
            EventCenter.Instance.RemoveEventListen(GameEventName.JumpMapTab, OnJumpMapTab);
        }

        protected override void InitUI()
        {
            m_RecordView = GetRect(HallPanelNotifications.RecordBg_RECT).GetComponent<RecordView>();
            m_GalleryView = GetRect(HallPanelNotifications.GalleryBg_RECT).GetComponent<GalleryView>();
            m_RecordView.Init(this);
            m_GalleryView.Init(this);

            // 适配顶部UI
            var margin = UIUtility.CalculateSafeAreaMargins();
            var rectTop = GetRect(HallPanelNotifications.Top_RECT);
            //var rectBottom = GetRect(HallPanelNotifications.Buttom_RECT);
            rectTop.anchoredPosition = new Vector2(0, -margin.top - 5);
            //rectBottom.anchoredPosition = new Vector2(0, margin.bottom);
        }

        private void OnEnable()
        {
            GetControl<Toggle>(HallPanelNotifications.Home_CTRL).isOn = true;

            OnPlayerStarChanged(0, GameDataMgr.Instance.TotalStarNum);
            OnPlayerCoinsChanged(0, GameDataMgr.Instance.TotalCoinsNum);
            OnCurLevelChanged(GameDataMgr.Instance.CurLevel);

            GetControl<Text>(HallPanelNotifications.StarNum_CTRL).text = GameDataMgr.Instance.TotalStarNum.ToString();

            OnChangedHallBg(GameDataMgr.Instance.CurHallBgInfo);
        }

        private void OnPlayerStarChanged(long old, long cur)
        {
            DOTween.To(() => old, x => GetControl<Text>(HallPanelNotifications.StarNum_CTRL).text = $"{x}", cur, 1f);
        }

        private void OnPlayerCoinsChanged(long old, long cur)
        {
            DOTween.To(() => old, x => GetControl<Text>(HallPanelNotifications.CoinNum_CTRL).text = $"{x}", cur, 1f);
        }

        private void OnCurLevelChanged(int curLv)
        {
            GetControl<Text>(HallPanelNotifications.Level_CTRL).text = $"{BaseDataMgr.Instance.GetLanguageValue("Level")} {curLv}";
        }

        private void OnChangedHallBg(GameDataModel.AssetInfo info)
        {
            // 需要设置背景，默认使用默认背景
            Sprite bgSprite = null;
            if (info == null)
                bgSprite = AssetCacheServer.Instance.GetDefaultBg();
            else
                bgSprite = AssetCacheServer.Instance.GetSprite(info.type, info.id, true);

            SetBackground(bgSprite);
        }

        private void OnJumpMapTab()
        {
            GetControl<Toggle>(HallPanelNotifications.Dairy_CTRL).isOn = true;
            GetRect(HallPanelNotifications.DairyBg_RECT).GetComponent<DairyView>().OpenFirst();
        }

        protected override void ClickBtnHandle(string name)
        {
            switch (name)
            {
                case HallPanelNotifications.SetBtn_CTRL:
                    UIUtility.DisplayPanel(SettingPanel.SettingPanelView.PANEL_KEY, "UI/SettingPanel/Prefabs/SettingPanel");
                    break;
                case HallPanelNotifications.StartBtn_CTRL:
                    // 这块判断是否需要进行选择，如果不需要则直接进入游戏
                    var data = GameDataMgr.Instance.GetLevel(GameDataMgr.Instance.CurLevel);
                    if (data.level > 1)
                    {
                        if (data.finalSelectIdx == -1)
                            UIUtility.DisplayPanel(ChoosePanel.ChoosePanelView.PANEL_KEY, "UI/ChoosePanel/Prefabs/ChoosePanel", data: data);
                        else
                            UIUtility.DisplayPanel(GameLoadingPanel.GameLoadingPanelView.PANEL_KEY, "UI/GameLoadingPanel/Prefabs/GameLoadingPanel", data: data);
                    }
                    else
                        UIUtility.DisplayPanel(GamePanel.GamePanelView.PANEL_KEY, "UI/GamePanel/Prefabs/GamePanel", data: data);
                    break;
            }
        }

        protected override void ClickToggleHandle(string name, bool isOn)
        {
            switch (name)
            {
                case HallPanelNotifications.Record_CTRL:
                case HallPanelNotifications.Home_CTRL:
                case HallPanelNotifications.Gallery_CTRL:
                case HallPanelNotifications.Dairy_CTRL:
                    GetRect(HallPanelNotifications.RecordBg_RECT).gameObject.SetActive(name.Equals(HallPanelNotifications.Record_CTRL));
                    GetRect(HallPanelNotifications.GalleryBg_RECT).gameObject.SetActive(name.Equals(HallPanelNotifications.Gallery_CTRL));
                    GetControl<Button>(HallPanelNotifications.StartBtn_CTRL).gameObject.SetActive(name.Equals(HallPanelNotifications.Home_CTRL));
                    GetControl<Text>(HallPanelNotifications.StarNum_CTRL).transform.parent.gameObject.SetActive(!name.Equals(HallPanelNotifications.Gallery_CTRL));
                    GetControl<Button>(HallPanelNotifications.SetBtn_CTRL).gameObject.SetActive(!name.Equals(HallPanelNotifications.Gallery_CTRL));
                    GetRect(HallPanelNotifications.DairyBg_RECT).gameObject.SetActive(name.Equals(HallPanelNotifications.Dairy_CTRL));
                    break;
                default:
                    m_GalleryView.ClickToggleHandle(name, isOn);
                    break;
            }

        }

        public void SetBackground(Sprite newBg)
        {
            var size = UIUtility.CalcSizeToFill((transform as RectTransform), newBg.texture);
            GetControl<Image>(HallPanelNotifications.ImgBg_CTRL).sprite = newBg;
            GetControl<Image>(HallPanelNotifications.ImgBg_CTRL).rectTransform.sizeDelta = new Vector2(size.width, size.height);
        }

        public void CutHomePage()
        {
            GetControl<Toggle>(HallPanelNotifications.Home_CTRL).isOn = true;
        }

        public void TopDisplayInfos(bool isTopShow, bool coin, bool star)
        {
            var coinObj = GetControl<Text>(HallPanelNotifications.CoinNum_CTRL).transform.parent.gameObject;
            var starObj = GetControl<Text>(HallPanelNotifications.StarNum_CTRL).transform.parent.gameObject;
            if (coin)
                UIUtility.DisplayTopObj(coinObj, isTopShow);

            if (star)
                UIUtility.DisplayTopObj(starObj, isTopShow);
        }
    }
}